#define _CRT_SECURE_NO_WARNINGS 1

#include "stack.h"

void StackInit(Stack* ps)
{
	assert(ps);
	ps->arr = NULL;
	ps->capacity = ps->top = 0;
}

void CheckCapacity(Stack* ps)
{
	assert(ps);
	if (ps->top == ps->capacity)
	{
		ps->capacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
		STDataType* tmp = (STDataType*)realloc(ps->arr, sizeof(STDataType) * ps->capacity);
		assert(tmp);
		ps->arr = tmp;
	}
}

void StackPush(Stack* ps, STDataType data)
{
	assert(ps);
	CheckCapacity(ps);
	ps->arr[ps->top] = data;
	ps->top++;
}

int StackSize(Stack* ps)
{
	assert(ps);
	return ps->top;
}


int StackEmpty(Stack* ps)
{
	assert(ps);
	return !(ps->top);
}

void StackPop(Stack* ps)
{
	assert(ps);
	if (!StackEmpty(ps))
	{
		ps->top--;
	}
}

STDataType StackTop(Stack* ps)
{
	assert(ps);
	assert(!StackEmpty(ps));
	return ps->arr[ps->top - 1];
}

void StackDestroy(Stack* ps)
{
	assert(ps);
	free(ps->arr);
	ps->arr = NULL;
	ps->capacity = ps->top = 0;
}

void StackPrint(Stack* ps)
{
	assert(ps);
	for (int i = 0; i < ps->top; i++)
	{
		printf("%d ", ps->arr[i]);
	}
	printf("\n");
}